
*** Replication data and do-file for
*** Donaubauer, J., Neumayer, E. and Nunnenkamp, P. (2019) Financial Market Development in Host and Source Countries and Its Effects on Bilateral FDI 
*** World Economy 
set more off

* set directory

***********************************************************************************

cd  "D:\Research\Development finance\Nunnenkamp\" 


/*change relative path to the directory where the files are located */

***********************************************************************************


use "Article for World Economy (FDI and FMD).dta", clear

tsset 

local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti l.ln_gdp_pc_desti

		
/* Table 1 */
* all hosts 
* unconditional
xtpoisson fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table1, replace excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES)
										   					   
 									
su fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti  if e(sample)

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table1, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

gen sample_total=1 if e(sample)

									   							   										  																			   
****************
* dev hosts only
preserve
replace inc_high_oecd_desti=0 if country_desti=="Slovenia"									   
keep if inc_high_oecd_desti==0

** One stage, fixed effects
* unconditional
* dest & origin infrastructure
xtpoisson fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table1, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 
										   
									
su fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti  if e(sample)

 
* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table1, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

restore	

	
	
/* Table 2 */								   
replace inc_high_oecd_desti=0 if country_desti=="Slovenia"									   
drop if inc_high_oecd_desti==1
						   									  
/* foreign bank presence */
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti  l.ln_gdp_pc_desti

* Generate top foreign banks presence dummy
capture drop top_banks_perc_desti
su banks_perc_desti, detail
gen top_banks_perc_desti=0
replace top_banks_perc_desti=1 if banks_perc_desti>=r(p75)
replace top_banks_perc_desti=. if banks_perc_desti==.

* dest & origin infrastructure
xtpoisson fdi_outstocks_rev2  `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0 & top_banks_perc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table2, replace excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0 & top_banks_perc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table2, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

/* top 3 investors */
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti  l.ln_gdp_pc_desti

* Generate rank of investors
capture drop fdi_outstocks_rev2_sum_dyad fdi_outstocks_rev2_rank
egen fdi_outstocks_rev2_sum_dyad=sum(fdi_outstocks_rev2), by(PV)
egen fdi_outstocks_rev2_rank=rank(-fdi_outstocks_rev2_sum_dyad), by(country_desti year)  unique


* dest & origin infrastructure
xtpoisson fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti   i.year if ofc_origin==0 & ofc_desti==0 & fdi_outstocks_rev2_rank>3, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table2, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent     i.year if ofc_origin==0 & ofc_desti==0 & fdi_outstocks_rev2_rank>3, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table2, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

									   
/* Table 3 */
preserve

replace inc_high_oecd_desti=0 if country_desti=="Slovenia"									   
keep if inc_high_oecd_desti==0
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti l.ln_gdp_pc_desti

* First and second stage

** 1st stage, fixed effects
* unconditional
* dest & origin infrastructure
xtlogit fdi_outstocks_rev2  `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0, fe 
cap noisily: outreg2 using Tables_graphs_paper/table3, replace excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES)  

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtlogit fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0 , fe 
cap noisily: outreg2 using Tables_graphs_paper/table3, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

** 2nd stage, fixed effects
* unconditional
* dest & origin infrastructure
xtreg ln_fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti    i.year  if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table3, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 


* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtreg ln_fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent     i.year  if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table3, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) ///  
 										   sortvar (`controls_lag' l.inf_finance_origin l.inf_finance_desti ///
										   L.inf_finance_origin_meancent L.inf_finance_desti_meancent cL.inf_finance_origin_meancent#cL.inf_finance_desti_meancent)
							   										   							   				 
										   							   							   
restore										   							   
   
										   
							   										   
/* Table 4 */
preserve

* Broader definition of developed country/narrower definition of developing country									   
replace inc_high_oecd_desti=1 if country_desti=="Slovenia"
replace inc_high_oecd_desti=1 if country_desti=="Chile"
replace inc_high_oecd_desti=1 if country_desti=="Estonia"
replace inc_high_oecd_desti=1 if country_desti=="Israel"
replace inc_high_oecd_desti=1 if country_desti=="Latvia"
replace inc_high_oecd_desti=1 if country_desti=="Lithuania"
									   
drop if inc_high_oecd_desti==1						   
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti  l.ln_gdp_pc_desti

* unconditional
xtpoisson fdi_outstocks_rev2  `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table4, replace excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table4, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

restore
										   
preserve

* Narrower definition of developed country/broader definition of developing country									   
replace inc_high_oecd_desti=0 if country_desti=="Czech Republic"
replace inc_high_oecd_desti=0 if country_desti=="Hungary"
replace inc_high_oecd_desti=0 if country_desti=="Poland"
replace inc_high_oecd_desti=0 if country_desti=="Korea, Rep."
replace inc_high_oecd_desti=0 if country_desti=="Slovenia"
replace inc_high_oecd_desti=0 if country_desti=="Slovak Republic"
									   
drop if inc_high_oecd_desti==1							   
										   
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti  l.ln_gdp_pc_desti

* dest & origin infrastructure
xtpoisson fdi_outstocks_rev2  `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table4, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table4, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

restore
							   
										   
/* Table 5 */									   	
* Exclude tax haven source countries
preserve

mmerge country_desti using 	"Article for World Economy (FDI and FMD) tax havens destinations.dta"									   
drop if _merge==-1
drop _merge

mmerge country_origin using 	"Article for World Economy (FDI and FMD) tax havens origins.dta"									   
drop if _merge==-1
drop _merge

tsset							   										   
								   
replace inc_high_oecd_desti=0 if country_desti=="Slovenia"							   
drop if inc_high_oecd_desti==1					   
drop if dest_tax_haven_doggart==1
drop if origin_tax_haven_doggart==1
										   
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti  l.ln_gdp_pc_desti

* dest & origin infrastructure
xtpoisson fdi_outstocks_rev2  `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table5, replace excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table5, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

restore
										   
preserve

* Exclude Argentina, Venezuela and Kazakhstan as source countries									   
replace inc_high_oecd_desti=0 if country_desti=="Slovenia"
									   
drop if inc_high_oecd_desti==1
drop if country_origin=="Argentina"
drop if country_origin=="Kazakhstan"
drop if country_origin=="Venezuela, RB"			   
										   
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti  l.ln_gdp_pc_desti

* dest & origin infrastructure
xtpoisson fdi_outstocks_rev2  `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table5, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table5, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

restore
										   
* exclude source countries with good FMD

bysort iso_o: egen inf_finance_origin_mean=mean(inf_finance_origin)
preserve
duplicates drop iso_o year, force
egen rank_fin_sampleperiod_origin = rank(-inf_finance_origin_mean) if year==2012
*gen top10_fin_sampleperiod_origin = 1 if rank_fin_sampleperiod_origin<11
save top_fin_sampleperiod_origin.dta, replace
restore

use "Article for World Economy (FDI and FMD).dta", clear
merge 1:1 iso_o iso_d year using top_fin_sampleperiod_origin.dta
drop _merge

bysort iso_o: egen rank_finance_origin=mean(rank_fin_sampleperiod_origin)
drop rank_fin_sampleperiod_origin


* dest & origin infrastructure
replace inc_high_oecd_desti=0 if country_desti=="Slovenia"						   
drop if inc_high_oecd_desti==1

tsset
										   
* exclude top 15
xtpoisson fdi_outstocks_rev2  `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0 & rank_finance_origin>15, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table5, append excel ///
cttop(exclude top15) ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0 & rank_finance_origin>15, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table5, append excel ///
cttop(exclude top15) ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 
										   										   									      
			   
/* Table 6 */
use "Article for World Economy (FDI and FMD).dta", clear
tsset
preserve
		   										   
* Exclude observations with negative FDI stock data								   
drop if fdi_outstocks_rev1<0

replace inc_high_oecd_desti=0 if country_desti=="Slovenia"								   
drop if inc_high_oecd_desti==1						   
										   
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti  l.ln_gdp_pc_desti


* dest & origin infrastructure
xtpoisson fdi_outstocks_rev2  `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table6, replace excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table6, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

restore

   										 
* Exclude observations with negative or zero FDI stock data	
preserve							   
drop if fdi_outstocks_rev1<=0

replace inc_high_oecd_desti=0 if country_desti=="Slovenia"
								   
drop if inc_high_oecd_desti==1								   

local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti  l.ln_gdp_pc_desti


* dest & origin infrastructure
xtpoisson fdi_outstocks_rev2  `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table6, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year if ofc_origin==0 & ofc_desti==0 , fe robust
cap noisily: outreg2 using Tables_graphs_paper/table6, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

restore


									   
******************************
* Table 7: additional controls
preserve
replace inc_high_oecd_desti=0 if country_desti=="Slovenia"									   
keep if inc_high_oecd_desti==0

gen ln_imports=log(imports)

* Generate capital stock per worker variables
capture drop ck_peremp_desti
gen ck_peremp_desti=ck_desti/emp_desti
capture drop rkna_peremp_desti
gen rkna_peremp_desti=rkna_desti/emp_desti

capture drop ck_peremp_origin
gen ck_peremp_origin=ck_desti/emp_origin
capture drop rkna_peremp_origin
gen rkna_peremp_origin=rkna_desti/emp_origin

* Generate difference in endowments variables
capture drop hc_diff_origin_desti
gen hc_diff_origin_desti=hc_origin-hc_desti
capture drop ck_diff_origin_desti
gen ck_diff_origin_desti=ck_peremp_origin-ck_peremp_desti
capture drop rkna_diff_origin_desti
gen rkna_diff_origin_desti=rkna_peremp_origin-rkna_peremp_desti

* bilateral, time-varying: trade, rta, bit_signed, dtt
local dyad_controls_timevarying_lag l.ln_imports l.rta l.bit_signed l.dtt l.hc_diff_origin_desti l.rkna_diff_origin_desti 
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti l.ln_gdp_pc_desti

* country-specific (host only)
local host_controls_lag l.WGI_rule_desti l.resources_gdp_desti 
 
* dest & origin infrastructure
xtpoisson fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year `host_controls_lag'  `dyad_controls_timevarying_lag' if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table7, replace excel ///
										   drop (d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

su fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti  if e(sample)

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent i.year `host_controls_lag'  `dyad_controls_timevarying_lag' if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table7, append excel ///
										   drop (d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) ///
										   sortvar (`controls_lag' l.inf_finance_origin l.inf_finance_desti ///
										   L.inf_finance_origin_meancent L.inf_finance_desti_meancent cL.inf_finance_origin_meancent#cL.inf_finance_desti_meancent ///
										   `dyad_controls_timevarying_lag' `host_controls_lag' `sourcehost_controls_lag' )

										   										   										   
* COUNTRY INSTEAD OF DYAD FE (+ bilateral dummies: distance, comlang, colony, contig) 
qui tab country_desti, gen(d_)
qui tab country_origin, gen(o_)

local dyad_controls ln_distw comlang_off colony contig

* dest & origin infrastructure
quietly xtpoisson fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year  `dyad_controls' `dyad_controls_timevarying_lag' `host_controls_lag' if ofc_origin==0 & ofc_desti==0, robust fe
xtpoisson fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year d_* o_* `dyad_controls' `dyad_controls_timevarying_lag' `host_controls_lag' if e(sample), robust
cap noisily: qui outreg2 using Tables_graphs_paper/table7, excel append ///
										   drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, NO, Country FE, YES, Time FE, YES) 				

su fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti  if e(sample)

* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)

quietly xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent  ///
i.year  `dyad_controls' `dyad_controls_timevarying_lag' `host_controls_lag' if ofc_origin==0 & ofc_desti==0,  robust fe
xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.inf_finance_origin_meancent##c.l.inf_finance_desti_meancent  ///
i.year d_* o_* `dyad_controls' `dyad_controls_timevarying_lag' `host_controls_lag' if e(sample),  robust
cap noisily: outreg2 using Tables_graphs_paper/table7, excel append ///
										   drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, NO, Country FE, YES, Time FE, YES) ///
 										   sortvar (`controls_lag' l.inf_finance_origin l.inf_finance_desti ///
										   L.inf_finance_origin_meancent L.inf_finance_desti_meancent cL.inf_finance_origin_meancent#cL.inf_finance_desti_meancent)											   
						   																	 

* HOST AND SOURCE COUNTRY-SPECIFIC YEAR FIXED EFFECTS
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti l.ln_gdp_pc_desti

xtpoisson fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0, fe robust
  
* conditional between dest & origin infrastructure
capture drop inf_finance_origin_meancent inf_finance_desti_meancent
su inf_finance_origin if e(sample)
gen inf_finance_origin_meancent=inf_finance_origin-r(mean)
su inf_finance_desti if e(sample)
gen inf_finance_desti_meancent=inf_finance_desti-r(mean)
capture drop interaction
gen interaction=l.inf_finance_origin_meancent*l.inf_finance_desti_meancent

* PPML Panel Structural Gravity Estimation
gen lag_ln_imports =  l.ln_imports
gen lag_rta = l.rta
gen lag_bit =  l.bit_signed
gen lag_dtt =  l.dtt
gen lag_hc_diff = l.hc_diff_origin_desti
gen lag_rkna_diff = l.rkna_diff_origin_desti 

ppml_panel_sg fdi_outstocks_rev2 interaction lag_ln_imports lag_rta lag_bit lag_dtt lag_hc_diff lag_rkna_diff  if ofc_origin==0 & ofc_desti==0, ex(iso_o) im(iso_d) y(year) maxiter(100000)

capture noisily: qui outreg2 using Tables_graphs_paper/table7, excel append ///
					auto(3) bdec(3) sdec(3) rdec(3) addtext(Pair FE, YES, Country-Time FE, YES) 

									   
restore										   
										   																					   
																					  																		  																					   
***********************************
* Table 8: Traditional FMD measures

preserve

replace inc_high_oecd_desti=0 if country_desti=="Slovenia"									   

keep if inc_high_oecd_desti==0

* baseline for comparison
local controls_lag l.ln_gdp_constant_origin l.ln_gdp_pc_origin l.ln_gdp_constant_desti l.ln_gdp_pc_desti
xtpoisson fdi_outstocks_rev2 `controls_lag' l.inf_finance_origin l.inf_finance_desti i.year if ofc_origin==0 & ofc_desti==0, fe robust
gen baseline_dummy=1 if e(sample)

*preserve
keep if baseline_dummy==1

* estimations without interaction	 
cap noisily: qui erase Tables_graphs_paper/table8.xml
cap noisily: qui erase Tables_graphs_paper/table8.txt	
	 
* as in Desbordes and Wei JDE 2017
gen prcredit_bankother_ln_origin=log(b_privatecredit_bankother_origin)
gen prcredit_bankother_ln_desti=log(b_privatecredit_bankother_desti)

gen stockcapital_ln_origin=log(b_stockcapital_origin)
gen stockcapital_ln_desti=log(b_stockcapital_desti)

local fin1 l.prcredit_bankother_ln_origin l.prcredit_bankother_ln_desti
local fin2 l.stockcapital_ln_origin l.stockcapital_ln_desti
	 	
* dest & origin infrastructure
forv i=1/2 {

xtpoisson fdi_outstocks_rev2 `controls_lag' `fin`i'' i.year if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table8, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 
}				   

								   								  																		   
* conditional between dest & origin infrastructure(D+W)								   
su fdi_outstocks_rev2 `controls_lag' `fin1' `fin2'  if e(sample)

capture drop prcredit_ln_origin_meancent prcredit_ln_desti_meancent
su prcredit_bankother_ln_origin if e(sample)
gen prcredit_ln_origin_meancent=prcredit_bankother_ln_origin-r(mean)
su prcredit_bankother_ln_desti if e(sample)
gen prcredit_ln_desti_meancent=prcredit_bankother_ln_desti-r(mean)

capture drop stockcapital_ln_origin_meancent stockcapital_ln_desti_meancent
su stockcapital_ln_origin if e(sample)
gen stockcapital_ln_origin_meancent=stockcapital_ln_origin-r(mean)
su stockcapital_ln_desti if e(sample)
gen stockcapital_ln_desti_meancent=stockcapital_ln_desti-r(mean)

* Desbordes and Wei, measure 1
xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.prcredit_ln_origin_meancent##c.l.prcredit_ln_desti_meancent i.year if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table8, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 
										   									   
* Desbordes and Wei, measure 2
xtpoisson fdi_outstocks_rev2 `controls_lag' c.l.stockcapital_ln_origin_meancent##c.l.stockcapital_ln_desti_meancent i.year if ofc_origin==0 & ofc_desti==0, fe robust
cap noisily: outreg2 using Tables_graphs_paper/table8, append excel ///
drop (/lnalpha d_* o.d_*  o_* o.o_* 2001.year 2002.year 2003.year 2004.year 2005.year 2006.year 2007.year 2008.year 2009.year 2010.year 2011.year 2012.year ) ///
										   addtext (Pair FE, YES, Country FE, NO, Time FE, YES) 

									   

